//
//  ZZPlayerToolBar.m
//  planning
//
//  Created by mac on 2019/7/30.
//  Copyright © 2019 mac. All rights reserved.
//

#import "ZZPlayerToolBar.h"

#import "ZZMusic.h"

#import "ZZAudioTool.h"

#import "NSString+ZZ.h"

#import "UIImage+ZZ.h"

//#import "Colours.h"

@interface  ZZPlayerToolBar()

@property (weak, nonatomic) UIImageView *bgImg;//背景图片

@property (weak, nonatomic) UIImageView *singerIcon;//歌手图片

@property (weak, nonatomic) UIButton *previousBtn;//上一首

@property (weak, nonatomic) UIButton *playBtn;//播放\暂停

@property (weak, nonatomic) UIButton *nextBtn;//下一首

@property (weak, nonatomic) UIButton *typeBtn;//播放模式

@property (weak, nonatomic) UISlider *timeSlider;//滚动条

@property (weak, nonatomic) UILabel *totalTimeLabel;//总时间

@property (weak, nonatomic) UILabel *currentTimeLabel;//当前播放的时间

@property (strong, nonatomic) CADisplayLink *link;//定时器

@property (assign, nonatomic, getter = isDragging) BOOL dragging;//是否正在拖拽

@end

@implementation ZZPlayerToolBar

- (CADisplayLink *)link {
    
    if (!_link) {
        
        _link = [CADisplayLink  displayLinkWithTarget:self  selector:@selector(update)];
        
    }
    
    return _link;
    
}

+ (instancetype)playerToolBar

{
    
    return [[self alloc] init];
    
}

- (instancetype)initWithFrame:(CGRect)frame

{
    
    if (self = [super initWithFrame:frame]) {
        
        [self  setUpSubViews];
        
        [self.link  addToRunLoop:[NSRunLoop  mainRunLoop] forMode:NSDefaultRunLoopMode];
        
    }
    
    return  self;
    
}

- (instancetype)initWithCoder:(NSCoder *)decoder

{
    
    if (self = [super initWithCoder:decoder]) {
        
        [self  setUpSubViews];
        
        [self.link  addToRunLoop:[NSRunLoop  mainRunLoop] forMode:NSDefaultRunLoopMode];
        
    }
    
    return  self;
    
}

#pragma mark - setUp 初始化

- (void)setUpSubViews

{
    
    UIImageView *bgImg = [[UIImageView  alloc] init];
    
    bgImg.image = [UIImage imageNamed:@"背景"];
    
    [self addSubview:bgImg];
    
    self.bgImg = bgImg;
    
    UISlider *timeSlider = [[UISlider alloc] init];
    
    [timeSlider setThumbImage:[UIImage  imageNamed:@"playbar_slider_thumb"] forState:UIControlStateNormal];
    
    [timeSlider addTarget:self  action:@selector(timeSliderChange:) forControlEvents:UIControlEventValueChanged];
    
    [self addSubview:timeSlider];
    
    self.timeSlider = timeSlider;
    
    UILabel *currentTimeLabel = [[UILabel alloc] init];
    
    currentTimeLabel.font = [UIFont systemFontOfSize:14.0f];
    
    currentTimeLabel.textColor = [UIColor whiteColor];
    
    currentTimeLabel.textAlignment = NSTextAlignmentRight;
    
    currentTimeLabel.text = NSLocalizedString(@"00:00", nil);
    
    [self addSubview:currentTimeLabel];
    
    self.currentTimeLabel = currentTimeLabel;
    
    UILabel *totalTimeLabel = [[UILabel alloc] init];
    
    totalTimeLabel.font = [UIFont systemFontOfSize:14.0f];
    
    totalTimeLabel.textColor = [UIColor lightGrayColor];
    
    totalTimeLabel.textAlignment = NSTextAlignmentLeft;
    
    totalTimeLabel.text = NSLocalizedString(@"00:00", nil);
    
    [self addSubview:totalTimeLabel];
    
    self.totalTimeLabel = totalTimeLabel;
    
    UIImageView *singerIcon = [[UIImageView alloc] init];
    
    singerIcon.image = [UIImage imageNamed:@"音乐.jpg"];
    
    [self addSubview:singerIcon];
    
    self.singerIcon = singerIcon;
    
    UIButton *previousBtn = [[UIButton alloc] init];
    
    [previousBtn setImage:[UIImage  imageNamed:@"上一曲"] forState:UIControlStateNormal];
    
    [previousBtn setImage:[UIImage  imageNamed:@"上一曲"] forState:UIControlStateHighlighted];
    
    [previousBtn addTarget:self  action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
    
    previousBtn.tag = ZZPlayerToolBarPreviousButtonType;
    
    [self addSubview:previousBtn];
    
    self.previousBtn = previousBtn;
    
    UIButton *playBtn = [[UIButton alloc] init];
    
    [playBtn setImage:[UIImage  imageNamed:@"开始"] forState:UIControlStateNormal];
    
    [playBtn setImage:[UIImage  imageNamed:@"开始"] forState:UIControlStateHighlighted];
    
    [playBtn addTarget:self  action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
    
    playBtn.tag = ZZPlayerToolBarPlayButtonType;
    
    [self addSubview:playBtn];
    
    self.playBtn = playBtn;
    
    UIButton *nextBtn = [[UIButton alloc] init];
    
    [nextBtn setImage:[UIImage  imageNamed:@"下一曲"] forState:UIControlStateNormal];
    
    [nextBtn setImage:[UIImage  imageNamed:@"下一曲"] forState:UIControlStateHighlighted];
    
    [nextBtn addTarget:self  action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
    
    nextBtn.tag = ZZPlayerToolBarNextButtonType;
    
    [self addSubview:nextBtn];
    
    self.nextBtn = nextBtn;
    
    UIButton *typeBtn = [[UIButton alloc] init];
    
    typeBtn.layer.borderWidth = 1;
    
    typeBtn.layer.borderColor = [UIColor  blueColor].CGColor;
    
    [typeBtn addTarget:self  action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
    
    typeBtn.tag = ZZPlayerToolBarTypeButtonType;
    
#warning - 没图先隐藏
    
    typeBtn.hidden = YES;
    
    [self addSubview:typeBtn];
    
    self.typeBtn = typeBtn;
    
}

- (void)update

{
    
    // 1.更新进度条
    
    double currentTime = [ZZAudioTool shareInstance].player.currentTime;
    
    self.timeSlider.value = currentTime;
    
    // 2.更新时间
    
    self.currentTimeLabel.text = [NSString getMinuteSecondWithSecond:currentTime];
    
}

/**
 
 *  时间滑动变化
 
 */

- (void)timeSliderChange:(UISlider *)slider

{
    
    // 1.播放器进度
    
    [ZZAudioTool shareInstance].player.currentTime = slider.value;
    
    // 2.工具条的当前时间
    
    self.currentTimeLabel.text = [NSString  getMinuteSecondWithSecond:slider.value];
    
}

/**
 
 *  点击按钮
 
 *
 
 *  @param button 根据按钮类型来判断
 
 */

- (void)buttonClick:(UIButton *)button

{
    
    if ([self.delegate respondsToSelector:@selector(playerToolBar:didClickBtnWithType:)]) {
        
        if (button.tag == ZZPlayerToolBarPlayButtonType) {
            
            if (self.music.playing) { // 播放
                
                self.music.playing = NO;
                
                [button setImage:[UIImage  imageNamed:@"开始"] forState:UIControlStateNormal];
                
                [button setImage:[UIImage  imageNamed:@"开始"] forState:UIControlStateHighlighted];
                
                button.tag = ZZPlayerToolBarPauseButtonType;
                
                [self.delegate  playerToolBar:self  didClickBtnWithType:button.tag];
                
            }
            
        } else  if (button.tag == ZZPlayerToolBarPauseButtonType) { // 暂停
            
            self.music.playing = YES;
            
            [button setImage:[UIImage  imageNamed:@"暂停"] forState:UIControlStateNormal];
            
            [button setImage:[UIImage  imageNamed:@"暂停"] forState:UIControlStateHighlighted];
            
            button.tag = ZZPlayerToolBarPlayButtonType;
            
            [self.delegate  playerToolBar:self  didClickBtnWithType:button.tag];
            
        } else  if (button.tag == ZZPlayerToolBarNextButtonType | button.tag == ZZPlayerToolBarPreviousButtonType) {
            
            [self.playBtn  setImage:[UIImage  imageNamed:@"下一曲"] forState:UIControlStateNormal];
            
            [self.playBtn  setImage:[UIImage  imageNamed:@"下一曲"] forState:UIControlStateHighlighted];
            
            [self.delegate  playerToolBar:self  didClickBtnWithType:button.tag];
            
        } else {
            
            [self.delegate  playerToolBar:self  didClickBtnWithType:button.tag];
            
        }
        
    }
    
}

/**
 
 *  拿到真实的尺寸进行子控件的布局
 
 */

- (void)layoutSubviews

{
    
#warning - 一定要调用super的方法
    
    [super  layoutSubviews];
    
    CGFloat margin = 5;
    
    self.bgImg.frame = self.frame;
    
    CGFloat currentTimeLabelX = 10;
    
    CGFloat currentTimeLabelY = 5;
    
    CGFloat currentTimeLabelW = 40;
    
    CGFloat currentTimeLabelH = 30;
    
    self.currentTimeLabel.frame = CGRectMake(currentTimeLabelX, currentTimeLabelY, currentTimeLabelW, currentTimeLabelH);
    
    CGFloat timeSliderX = CGRectGetMaxX(self.currentTimeLabel.frame) + margin;
    
    CGFloat timeSliderY = currentTimeLabelY;
    
    CGFloat timeSliderW = self.frame.size.width - 2 * (currentTimeLabelW + currentTimeLabelX + margin);
    
    CGFloat timeSliderH = currentTimeLabelH;
    
    self.timeSlider.frame = CGRectMake(timeSliderX, timeSliderY, timeSliderW, timeSliderH);
    
    CGFloat totalTimeLabelX = self.frame.size.width - currentTimeLabelW - currentTimeLabelX;
    
    CGFloat totalTimeLabelY = currentTimeLabelY;
    
    CGFloat totalTimeLabelW = currentTimeLabelW;
    
    CGFloat totalTimeLabelH = currentTimeLabelH;
    
    self.totalTimeLabel.frame = CGRectMake(totalTimeLabelX, totalTimeLabelY, totalTimeLabelW, totalTimeLabelH);
    
    CGFloat singerIconX = currentTimeLabelX;
    
    CGFloat singerIconY = CGRectGetMaxY(self.timeSlider.frame) + margin;
    
    CGFloat singerIconW = 50;
    
    CGFloat singerIconH = singerIconW;
    
    self.singerIcon.frame = CGRectMake(singerIconX, singerIconY, singerIconW, singerIconH);
    
    CGFloat previousBtnX = CGRectGetMaxX(self.singerIcon.frame) + 25;
    
    CGFloat previousBtnY = singerIconY;
    
    CGFloat previousBtnW = 50;
    
    CGFloat previousBtnH = previousBtnW;
    
    self.previousBtn.frame = CGRectMake(previousBtnX, previousBtnY, previousBtnW, previousBtnH);
    
    CGFloat playBtnX = CGRectGetMaxX(self.previousBtn.frame) + 25;
    
    CGFloat playBtnY = singerIconY - 5;
    
    CGFloat playBtnW = 60;
    
    CGFloat playBtnH = playBtnW;
    
    self.playBtn.frame = CGRectMake(playBtnX, playBtnY, playBtnW, playBtnH);
    
    CGFloat nextBtnX = CGRectGetMaxX(self.playBtn.frame) + 25;
    
    CGFloat nextBtnY = singerIconY;
    
    CGFloat nextBtnW = previousBtnW;
    
    CGFloat nextBtnH = previousBtnW;
    
    self.nextBtn.frame = CGRectMake(nextBtnX, nextBtnY, nextBtnW, nextBtnH);
    
    CGFloat typeBtnX = CGRectGetMaxX(self.nextBtn.frame) + 20;
    
    CGFloat typeBtnY = singerIconY;
    
    CGFloat typeBtnW = previousBtnW;
    
    CGFloat typeBtnH = previousBtnW;
    
    self.typeBtn.frame = CGRectMake(typeBtnX, typeBtnY, typeBtnW, typeBtnH);
    
}

#pragma mark - setter 赋值&mvc思想

- (void)setMusic:(ZZMusic *)music

{
    
    _music = music;
    
    // 设置歌手图片数据
    
    [self  setUpSingerIconData];
    
    // 设置滑动条的数据
    
    [self  setUpSliderData];
    
}

/**
 
 *  设置歌手图片数据
 
 */

- (void)setUpSingerIconData

{
    
    // 0.设置圆角
    self.singerIcon.image = [UIImage  circleImageWithName:self.music.singerIcon  borderWidth:2  borderColor:[UIColor yellowColor]];
    
#warning - 后期再补图片旋转
    
    //    // 1.单位矩阵
    
    //    self.singerIcon.transform = CGAffineTransformIdentity;
    
}

/**
 
 *  设置滑动条的数据
 
 */

- (void)setUpSliderData

{
    
    // 0.去除当前播放歌曲的总时间
    
    double duration = [ZZAudioTool shareInstance].player.duration;
    
    // 1.将时间转换&设置时间属性
    
    self.totalTimeLabel.text = [NSString  getMinuteSecondWithSecond:duration];
    
    // 2.设置slider的最大值
    
    self.timeSlider.maximumValue = duration;
    
    // 3.重置slider的播放时间
    
    self.timeSlider.value = 0;
    
}

- (void)dealloc

{
    
    //移除定时器
    [self.link  removeFromRunLoop:[NSRunLoop  mainRunLoop] forMode:NSDefaultRunLoopMode];
    
}

@end
